/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzilla.desk;

import com.yy.bugzilla.desk.tool.BasePane;
import com.yy.bugzillahelp.action.BugAction;
import com.yy.bugzillahelp.data.BugDetailData;
import com.yy.bugzillahelp.data.BugStatus;
import com.yy.bugzillahelp.data.server.ServerData;
import com.yy.bugzillahelp.db.BugzillaHelpDB;
import com.yy.bugzillahelp.tool.SQLTool;
import java.sql.Connection;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

/**
 *
 * @author Alan
 */
public class BugListPane extends BasePane {

    public static final String BUG_LIST = "Bug列表";
    private ObservableList<BugDetailData> bugDetailDatas = FXCollections.observableArrayList();
    private MainApp mainApp;
    private ServerData serverData;
    private TableView<BugDetailData> tableView;
    private ContextMenu contextMenu;
    private BugStatus[] status;

    public BugListPane(MainApp mainApp, ServerData serverData) {
        this.mainApp = mainApp;
        this.serverData = serverData;
    }

    public void setBugStatus(BugStatus... status) {
        this.status = status;
    }

    @Override
    protected Pane generatePane() {
        initDatas();

        BorderPane borderPane = new BorderPane();
        tableView = new TableView<>(bugDetailDatas);
        TableColumn<BugDetailData, String> id = createTableColumn("id");
        id.setMinWidth(60);
        id.setMaxWidth(60);
        tableView.getColumns().add(id);
        TableColumn<BugDetailData, String> shortDesc = createTableColumn("shortDesc");
        shortDesc.setMinWidth(400);
        shortDesc.setMaxWidth(2000);
        tableView.getColumns().add(shortDesc);
        TableColumn<BugDetailData, String> product = createTableColumn("product");
        tableView.getColumns().add(product);
        tableView.getColumns().add(createTableColumn("component"));
        tableView.getColumns().add(createTableColumn("version"));
        tableView.getColumns().add(createTableColumn("status"));
        TableColumn<BugDetailData, String> resolution = createTableColumn("resolution");
        resolution.setVisible(false);
        tableView.getColumns().add(resolution);
        tableView.getColumns().add(createTableColumn("priority"));
        TableColumn<BugDetailData, String> severity = createTableColumn("severity");
        severity.setVisible(false);
        tableView.getColumns().add(severity);
        tableView.getColumns().add(createTableColumn("reporter"));
        tableView.getColumns().add(createTableColumn("assignedTo"));

        tableView.setTableMenuButtonVisible(true);
        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        generateContextMenu();
        tableView.setContextMenu(contextMenu);

        borderPane.setCenter(tableView);

        return borderPane;
    }

    private void initDatas() {
        Connection con = BugzillaHelpDB.getConnection();
        ArrayList<BugDetailData> datas = BugAction.searchByStatus(con, serverData.getId(), status);
        SQLTool.closeConnection(con);

        bugDetailDatas.clear();
        bugDetailDatas.addAll(datas);
    }

    public void reflush() {
        initDatas();
    }

    public ObservableList<BugDetailData> getBugDetailDatas() {
        return bugDetailDatas;
    }

    private BugDetailData getSelectedBugDetailData() {
        BugDetailData selectedItem = tableView.getSelectionModel().getSelectedItem();
        return selectedItem;
    }

    private TableColumn<BugDetailData, String> createTableColumn(String fieldName) {
        TableColumn<BugDetailData, String> tableColumn = new TableColumn<>(convertHeaderName(fieldName));
        tableColumn.setCellValueFactory(new PropertyValueFactory(fieldName));
        tableColumn.setMinWidth(100);
        tableColumn.setMaxWidth(100);
        return tableColumn;
    }

    private String convertHeaderName(String fieldName) {
        if (fieldName == null) {
            return "";
        }
        fieldName = fieldName.trim();
        if (fieldName.isEmpty()) {
            return "";
        }
        if (fieldName.length() <= 2) {
            return fieldName.toUpperCase();
        }

        StringBuilder sb = new StringBuilder(fieldName);
        sb.setCharAt(0, String.valueOf(sb.charAt(0)).toUpperCase().charAt(0));
        for (int i = sb.length() - 1; i > 0; i--) {
            char charAt = sb.charAt(i);
            if (charAt <= 'Z' && charAt >= 'A') {
                sb.insert(i, ' ');
            }
        }
        return sb.toString();
    }

    private void generateContextMenu() {
        contextMenu = new ContextMenu() {

            @Override
            protected void show() {
                BugDetailData bugDetailData = getSelectedBugDetailData();
                if (bugDetailData == null) {
                } else {
                    super.show();
                }
            }
        };
        contextMenu.getItems().add(createMenuItem("查看/编辑"));
    }

    private MenuItem createMenuItem(final String text) {
        MenuItem mi = new MenuItem(text);
        mi.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                view();
            }
        });
        return mi;
    }

    private void view() {
        BugDetailData detailData = getSelectedBugDetailData();
        if (detailData != null) {
            BugDetailPane detailPane = new BugDetailPane(serverData, detailData.getBugId());
            String tabName = BugDetailPane.BUG + " " + detailData.getBugId();
            if (mainApp.getMainTabPane().containsTab(tabName)) {
                mainApp.getMainTabPane().selectTab(tabName);
            } else {
                mainApp.getMainTabPane().addTab(tabName, detailPane.getContextPane());
            }
        }
    }
}
